package com.wx.springboot.step6.component.mq;

import com.wx.springboot.step6.enums.QueueEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 取消订单消息的发出者
 */
@Component
@Slf4j
public class OrderCancelSender {
    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 发送取消订单延迟消息
     * @param orderId
     * @param delayTimes
     */
    public void sendMessage(Long orderId, final long delayTimes){
        //给延迟队列发送消息
        amqpTemplate.convertAndSend(QueueEnum.QUEUE_ORDER_CANCEL_DELAY.getExchange(), QueueEnum.QUEUE_ORDER_CANCEL_DELAY.getRouteKey(), orderId,
                message -> {
                    //给消息设置延迟毫秒值
                    message.getMessageProperties().setExpiration(String.valueOf(delayTimes));
                    return message;
                });
        log.info("------------  消息推送成功 ------------");
        log.info("send delay message orderId:{}", orderId);
    }
}
